package com.bwie.service.impl;

import cn.hutool.core.util.StrUtil;
import com.bwie.common.result.Result;
import com.bwie.mapper.EsMapper;
import com.bwie.pojo.GoodsPoEs;
import com.bwie.service.EsService;
import org.dromara.easyes.core.biz.EsPageInfo;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.metrics.Max;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class EsServiceImpl implements EsService {
    // 注入EsMapper
    @Autowired
    private EsMapper esMapper;

    // 创建索引
    @Override
    public void createIndex() {
        esMapper.createIndex();
    }

    // 添加数据
    @Override
    public void add(GoodsPoEs goodsPoEs) {
        esMapper.insert(goodsPoEs);
    }

    // 删除数据
    @Override
    public void delete(Integer goodsId) {
        esMapper.deleteById(goodsId);
    }

    // 更新数据
    @Override
    public void update(GoodsPoEs goodsPoEs) {
        esMapper.updateById(goodsPoEs);
    }

    // 查询最大值
    @Override
    public void max() {
        LambdaEsQueryWrapper<GoodsPoEs> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.max(GoodsPoEs::getGoodsPrice);
        SearchResponse search = esMapper.search(wrapper);
        Max max = search.getAggregations().get("goodsPriceMax");
        System.out.println("最大值==================="+max.value());
    }

    // 查询数据
    @Override
    public Result search(GoodsPoEs goodsPoEs) {
        LambdaEsQueryWrapper<GoodsPoEs> wrapper = new LambdaEsQueryWrapper<>();
        if(StrUtil.isNotBlank(goodsPoEs.getGoodsName())){
            wrapper.match(GoodsPoEs::getGoodsName,goodsPoEs.getGoodsName());
        }
        EsPageInfo<GoodsPoEs> pageInfo = esMapper.pageQuery(wrapper, goodsPoEs.getPageNum(), goodsPoEs.getPageSize());
        System.out.println(pageInfo);
        return Result.success(pageInfo);
    }

    // 分组查询
    @Override
    public void group() {
        // 创建LambdaEsQueryWrapper对象，并设置分组条件
        LambdaEsQueryWrapper<GoodsPoEs> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.groupBy(GoodsPoEs::getGoodsId);
        // 执行查询
        SearchResponse search = esMapper.search(wrapper);
        // 获取分组结果
        Terms goodsName = search.getAggregations().get("goodsNameTerms");
        // 获取分组结果中的桶
        List<? extends Terms.Bucket> buckets = goodsName.getBuckets();
        // 创建一个ArrayList用于存储分组结果
        ArrayList<GoodsPoEs> list = new ArrayList<>();
        // 遍历桶
        for (Terms.Bucket bucket : buckets) {
            // 创建GoodsPoEs对象
            GoodsPoEs goodsPoEs = new GoodsPoEs();
            // 设置商品名称
            goodsPoEs.setGoodsName(bucket.getKeyAsString());
            // 将商品对象添加到列表中
            list.add(goodsPoEs);
        }
        // 输出分组结果
        System.out.println(list);
    }
}
